import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import javax.swing.*;
import java.util.*;

public class Animation_Window extends JFrame implements Observer {
	ArrayList<Figur> f = new ArrayList<Figur>();
    // The object we will use to write with instead of the standard screen graphics
    Graphics bufferGraphics;
    // The image that will contain everything that has been drawn on
    // bufferGraphics.
    Image offscreen;
    Window_Controller wcon;
    
	public Animation_Window(Window_Controller wcon) { 
		super("Animation");
		this.wcon = wcon;
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setSize(new Dimension(500,500));
		setVisible(true);
        offscreen = createImage(getSize().width,getSize().height);
        bufferGraphics = offscreen.getGraphics();
		setVisible(false);
	}

	private void draw (Image parent, Figur f) {
   		if (f.billede.getHeight(null) < 0) {
   			return;
   		}

   		GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();				
		GraphicsConfiguration gc = gd.getDefaultConfiguration();

   		BufferedImage child2 = (BufferedImage) createImage(getSize().width,getSize().height);
   		
   		child2 = gc.createCompatibleImage(child2.getWidth(), child2.getHeight(), Transparency.BITMASK);
   		Graphics2D cg2 = child2.createGraphics();
   		
   		//Image child = createImage(getSize().width,getSize().height);
	    //Graphics childGraphics = child2.getGraphics();
	    //Graphics2D cg2 = (Graphics2D)childGraphics;
   		
	    //Composite originalComposite = cg2.getComposite();
	    //cg2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
	    //cg2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
	    //Rectangle2D.Double rect = new Rectangle2D.Double(0,0,getSize().width, getSize().height);
	    //cg2.fill(rect);
	    
   		//cg2.setComposite(originalComposite);
	    cg2.drawImage(f.billede,f.x,f.y,null);
		
   		for (int i = 0; i<f.child.size(); i++) {
			draw (child2, f.child.get(i));
		}
		
   		AffineTransform transformer = AffineTransform.getRotateInstance(Math.toRadians((double)f.rotation),
   				(double)f.centerx,
   				(double)f.centery);
   		
		Graphics parentGraphics = parent.getGraphics();
   		Graphics2D pg2 = (Graphics2D)parentGraphics;
   		pg2.setTransform(transformer);
   		//pg2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f));
   		pg2.drawImage(child2,0,0,null);
	}
	
    /**
     * Paint a frame of animation.
     */
	public void paint(Graphics g) {
		while(bufferGraphics == null);
	   	bufferGraphics.clearRect(0, 0, getSize().width, getSize().height);
	   	
	   	for (int i=0; i<f.size(); i++) {
	   		draw(offscreen, f.get(i));
	   	}
		
        // draw the offscreen image to the screen like a normal image.
        // Since offscreen is the screen width we start at 0,0.
		g.drawImage(offscreen,0,0,this);
	}
    /**
     * Paint a frame of animation.
     */
    public void update(Graphics g) {
    	paint(g);
    }

	/**
	 * Metode til at opdatere (JFrame)Animation_Window.
	 */
	public void update(Observable o, Object arg) {
		Verden verden = (Verden)o;
		if (arg != null && (Boolean) arg == Boolean.TRUE) {
			//wcon.show_WorldCreator();
			verden.deleteObserver(this);
			wcon.hide_Animation_Window();
		}
		
		f = verden.getFigur();
		
		this.validate();
		this.repaint();
	}
}